//
//  CodeButton.m
//  AiShangJinFuSalesman
//
//  Created by AiShangTong on 2017/6/7.
//  Copyright © 2018年 AiShangTong. All rights reserved.
//

#import "CodeButton.h"

@interface CodeButton ()

@property (nonatomic,weak)NSTimer * countDownTimer;
@property (nonatomic,assign)NSInteger  countDownSeconds;

@end

@implementation CodeButton

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setBtnCSS];
    }
    return  self;
}

-(void)setBtnCSS
{
    self.backgroundColor = [UIColor whiteColor];
    self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self setTitleColor:ThemeMaoColor forState:UIControlStateNormal];
    [self.titleLabel setFont:[UIFont systemFontOfSize:13]];
}


-(void)setBtnSelect:(BOOL)btnSelect
{
    if (btnSelect) {
        
        if ([HFQCommonTools getVerificationCodeCountDown] > 60) {
            self.countDownSeconds = 60;//60秒倒计时
            [QuickDataLocalStorageTool WriteVerificationCodeCountDownWithStr:[HFQCommonTools getSystemCurrentTime]];
        }else {
            NSInteger differTime = [HFQCommonTools getVerificationCodeCountDown];
            self.countDownSeconds = 60-differTime;//60秒倒计时
        }
        
        [self setTitleColor:ThemeMaoColor forState:UIControlStateNormal];
        self.layer.borderColor = ThemeMaoColor.CGColor;
        self.userInteractionEnabled = NO;
        self.titleLabel.adjustsFontSizeToFitWidth = YES;
        [self setTitle:[NSString stringWithFormat:@"%ld秒",(long)self.countDownSeconds] forState:UIControlStateNormal];
        if (_countDownTimer == nil) {
            
            _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
        }
        
        
    } else {
        
        [self cancelTimeCount];
        
    }
}

-(void)timeFireMethod{
    
    _countDownSeconds--;
    
    [self setTitle:[NSString stringWithFormat:@"%ld秒",(long)self.countDownSeconds] forState:UIControlStateNormal];
    if( self.countDownSeconds == 0){
        
        [self stopTime];
    }
    
}

-(void)cancelTimeCount
{
    if (self.countDownTimer.isValid) {
        
        
        [self.countDownTimer invalidate];
        self.countDownTimer = nil;
        self.userInteractionEnabled = YES;
        [self setTitle:@"发送验证码" forState:UIControlStateNormal];
        [self setTitleColor:ThemeMaoColor forState:UIControlStateNormal];
        
    }
    
}

-(void)stopTime
{
    if (self.countDownTimer.isValid) {
        
        [self.countDownTimer setFireDate:[NSDate distantFuture]];
        [self.countDownTimer invalidate];
        self.countDownTimer = nil;
    }
    
    self.userInteractionEnabled = YES;
    [self setTitle:@"重新发送" forState:UIControlStateNormal];
    [self setTitleColor:ThemeMaoColor forState:UIControlStateNormal];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"DuanXinYanZhengKeChongXinFaSong" object:nil];
}


-(void)dealloc
{
    [self.countDownTimer invalidate];
    self.countDownTimer = nil;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
